﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using DevExpress.XtraEditors;
using Common;
using CNCSoft.EData;

using System.Threading;
using System.Globalization;

namespace TMS
{
    public partial class frmDanhMuc : Form
    {
        SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        SqlDataAdapter dataAdapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        string _strTenDanhMuc = "";
        string _strSQLSELECT = "";   //Cau lenh select tra ve de buil cac cau lenh Update/Insert/Delete tuong ung

        public frmDanhMuc()
        {
            InitializeComponent();
        }

        public frmDanhMuc(string TenDanhMuc)
        {
            // Sets the UI culture to VietNamese (VietNam) / English
            if (GlobalVariable.WSInfo.Language == "Tiếng Việt")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
            else if (GlobalVariable.WSInfo.Language == "English")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");


            InitializeComponent();
            _strTenDanhMuc = TenDanhMuc.Replace("\"", "") ;
        }

        private void DanhMuc_Load(object sender, EventArgs e)
        {
            FillData();
            XulyCotChonTuDanhMucKhac();

            gridViewDanhMuc.OptionsFind.AlwaysVisible = chkShowFindPanel.Checked;
            gridViewDanhMuc.OptionsView.ShowGroupPanel = chkShowGroupPanel.Checked;
            gridViewDanhMuc.OptionsView.ShowAutoFilterRow = chkShowFilterPanel.Checked;
        }

        
        private void FillData()
        {
            if (ds.Tables["DANHMUC"] != null)
                ds.Tables["DANHMUC"].Clear();

            SqlCommand cm = cnn.CreateCommand();
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "dbo.spfrmDanhMuc";
            SqlParameter tenDM = cm.Parameters.Add("@Activity", SqlDbType.VarChar, 500);
            tenDM.Value = _strTenDanhMuc;

            //Lay cau SELE ra lai (de luc update co cau SELE de build)
            SqlParameter SELEReturn = cm.Parameters.Add("@strSQLReturn", SqlDbType.VarChar, 1000);
            SELEReturn.Direction = ParameterDirection.Output;

            try
            {
                cnn.Open();
                dataAdapter = new SqlDataAdapter(cm);
                dataAdapter.Fill(ds, "DANHMUC");
           
                _strSQLSELECT = SELEReturn.Value.ToString();

                /*
                string strDANHMUC = "dbo.spfrmDanhMuc @Activity = '" + _strTenDanhMuc + "'" ;
                dataAdapter = new SqlDataAdapter(strDANHMUC, GlobalVariable.WSInfo.ConnectionString);
                dataAdapter.Fill(ds, "DANHMUC");
                */

                gridDanhMuc.DataSource = ds;
                gridDanhMuc.DataMember="DANHMUC" ;

            }
            catch (Exception ex)
            {
                MessageBox.Show("Không có danh mục, lỗi " + dataAdapter.SelectCommand.CommandText + ex.Message);
            }
        }

        private void XulyCotChonTuDanhMucKhac()
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn gc in gridViewDanhMuc.Columns)
            {
                //MessageBox.Show(gc.FieldName.ToUpper() );

                //Heder dam
                Font itemFont = new Font(gc.AppearanceHeader.Font, FontStyle.Bold);
                gc.AppearanceHeader.Font = itemFont;
                gc.AppearanceHeader.Options.UseFont = true;

                //Gia tri duoc chon tu DM khac
                string strTenDanhMuc = gc.FieldName.ToUpper().Replace("MA", "DM_");
                string strMA = gc.FieldName;
                string strTEN = strMA.Replace("Ma", "Ten");;
                
                //Cac truong hop dac biet
                string strChuoiFielCanXulyDM = "TA, GIAOVIENCHUNHIEM, NGUOIPHUTRACH";
                if (strChuoiFielCanXulyDM.IndexOf(gc.FieldName.ToUpper()) >= 0)  //TA chinh la lay tu DM_GIAOVIEN
                {
                    strTenDanhMuc = "DM_GIAOVIEN"; strMA = "MaGiaoVien"; strTEN = "TenGiaoVien";
                }

                //Truong hop MA_... va DM_... trung nhau (thong thuong)
                if (gc.FieldName.ToUpper().IndexOf("MA") >= 0 || strChuoiFielCanXulyDM.IndexOf(gc.FieldName.ToUpper()) >=0 )
                {
                    //MessageBox.Show(gc.FieldName);
                    if (strTenDanhMuc.ToUpper() != _strTenDanhMuc.ToUpper())
                    {
                        try
                        {
                            DataTable dtDM_Parent = new DataTable();
                            SQLHelper.Fill(dtDM_Parent, GlobalVariable.WSInfo.ConnectionString, "SELECT " + strMA + ", " + strTEN + " FROM " + strTenDanhMuc);
                            DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit Repository_ChonFromDM = new DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit();
                            Repository_ChonFromDM.DataSource = dtDM_Parent;
                            Repository_ChonFromDM.DisplayMember = strTEN;
                            Repository_ChonFromDM.ValueMember = strMA;
                            Repository_ChonFromDM.ShowDropDown = DevExpress.XtraEditors.Controls.ShowDropDown.DoubleClick;
                            gridDanhMuc.RepositoryItems.Add(Repository_ChonFromDM);
                            gc.ColumnEdit = Repository_ChonFromDM;
                            gc.ColumnEdit.NullText = "";
                        }
                        catch { }
                    }
                }

                //Format cac fiel kieu SO
                if (gc.ColumnType.FullName == "System.Int32" || gc.ColumnType.FullName == "System.Decimal" || gc.ColumnType.FullName == "System.Double")  
                {
                    //MessageBox.Show(gc.Name);
                    try
                    {
                        DevExpress.XtraEditors.Repository.RepositoryItemTextEdit Repository_Money = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
                        Repository_Money.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
                        Repository_Money.Mask.EditMask = "N0";
                        Repository_Money.Mask.UseMaskAsDisplayFormat = true;
                        gridDanhMuc.RepositoryItems.Add(Repository_Money);
                        gc.ColumnEdit = Repository_Money;
                        gc.ColumnEdit.NullText = "";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                //Format cac fiel kieu NGAY
                if (gc.ColumnType.FullName == "System.DateTime" )
                {
                    //MessageBox.Show(gc.Name);
                    try
                    {
                        DevExpress.XtraEditors.Repository.RepositoryItemTextEdit Repository_Date = new DevExpress.XtraEditors.Repository.RepositoryItemDateEdit();
                        Repository_Date.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.DateTime;
                        Repository_Date.Mask.EditMask = "dd/MM/yyyy";
                        Repository_Date.Mask.UseMaskAsDisplayFormat = true;
                        gridDanhMuc.RepositoryItems.Add(Repository_Date);
                        gc.ColumnEdit = Repository_Date;
                        gc.ColumnEdit.NullText = "";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                //An cot ID
                if (gc.FieldName.ToUpper() == "ID")
                    gc.Visible = false;
            }
        }

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
                //SqlDataAdapter sqldaDM = new SqlDataAdapter("SELECT * FROM " + _strTenDanhMuc, cnn);
                SqlDataAdapter sqldaDM = new SqlDataAdapter(_strSQLSELECT, cnn);
                DataHelper.buildDataCommands(ref sqldaDM);
                sqldaDM.Update(ds, "DANHMUC");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            ds.RejectChanges();
        }

        private void chkShowFilterPanel_CheckedChanged(object sender, EventArgs e)
        {
            gridViewDanhMuc.OptionsView.ShowAutoFilterRow = chkShowFilterPanel.Checked;
        }

        private void chkShowGroupPanel_CheckedChanged(object sender, EventArgs e)
        {
            gridViewDanhMuc.OptionsView.ShowGroupPanel = chkShowGroupPanel.Checked;
        }

        private void chkShowFindPanel_CheckedChanged(object sender, EventArgs e)
        {
            gridViewDanhMuc.OptionsFind.AlwaysVisible = chkShowFindPanel.Checked;
        }


    }
}
